#include"StdAfx.h"
#include"UnZipTool.h"

UnZipTool::UnZipTool()
{
	zip = NULL;
	current = 0;
	total = 0;
	percentage = 0;
}

void UnZipTool::Open(TString strZipSrc, TString _strDirPath, TString pwd)
{
	strDirPath = _strDirPath;
	zip = OpenZip(strZipSrc.c_str(), pwd.GetString().c_str());
	GetZipItem(zip, -1, &ze);
	total = ze.index;
	current = 0;
	percentage = 0;
}

void UnZipTool::Open(LPVOID pBuffer, size_t sz, TString _strDirPath, TString pwd)
{
	strDirPath = _strDirPath;
	zip = OpenZip(pBuffer,sz, pwd.GetString().c_str());
	GetZipItem(zip, -1, &ze);
	total = ze.index;
	current = 0;
	percentage = 0;
}

bool UnZipTool::GetOne()
{
	if (current>= total)
	{
		return false;
	}
	GetZipItem(zip, current, &ze);
	path DirPath(strDirPath.GetString());
	path filePath(W2A(ze.name));
	path RelPath(DirPath);
	RelPath.append(filePath.string());
	UnzipItem(zip, current, A2W(RelPath.string()).c_str());
	current++;
	percentage = (static_cast<float>(current) / total) * 100;
	strProgress = ze.name;
	return true;
}

void UnZipTool::MoveFirst()
{
	current = 0;
}

void UnZipTool::Close()
{
	CloseZip(zip);
}
